"""This script try to parse a dffmsd file"""

import sys
from kuai.anaylsis import diffmol
from kuai.dff import dffopt
import kuai

DFF_BIN = r"F:\Madlee\Job\Direct Force Field\7\bin32w"
PPF_FILE = r"F:\Madlee\Job\MayBridge\AMBER3.ppf"
MSD_FILE = r"F:\Madlee\Job\MayBridge\Optimize\sampleRHF.msd"
NAME_TAG = ">  <code> ("

def parse_name(block):
    i1 = block.find(NAME_TAG) + len(NAME_TAG)
    i2 = block.find(")", i1+1)
    return block[i1:i2].strip()


if __name__ == '__main__':
    if len(sys.argv) == 4:
        print __doc__
    else:
        ff, eqt = kuai.read_ff(PPF_FILE)
        input = open(MSD_FILE)
        block = ""
        for i in input:
            block += i
            if i.startswith("$$$$"):
                mol1 = kuai.parse_mol(block, "msd")
                mol1.name = parse_name(block)
                kuai.assign_charges(mol1, ff, eqt)
                mol2 = dffopt.optimize(mol1, ff, {"DFF_BIN":DFF_BIN})
                diffmol.compare_mol(mol1, mol2)
                block = ""
